﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using WCFAlmacen.Dominio;
using System.Data.SqlClient;

namespace WCFAlmacen.Persistencia
{
    public class GRSubCategoriaDAO
    {
        public List<GRSubCategoria> ListarSubCategorias()
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            List<GRSubCategoria> ListSubCategorias = null;
            GRSubCategoria objSubCategoria = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GRSubCategoria_Listar", con);
                cmd.CommandType = CommandType.StoredProcedure;
                rdr = cmd.ExecuteReader();

                if (rdr.HasRows)
                {
                    ListSubCategorias = new List<GRSubCategoria>();
                    while (rdr.Read())
                    {
                        objSubCategoria = new GRSubCategoria();
                        objSubCategoria.iCodSub = Convert.ToInt32(rdr["iCodSub"].ToString());
                        objSubCategoria.iCodCat = Convert.ToInt32(rdr["iCodCat"].ToString());
                        objSubCategoria.vNomSub = rdr["vNomSub"].ToString();
                        objSubCategoria.vDesSub = rdr["vDesSub"].ToString();
                        objSubCategoria.bActivoSub = Convert.ToBoolean(rdr["bActivoSub"].ToString());
                        objSubCategoria.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objSubCategoria.cNomTer = rdr["cNomTer"].ToString();
                        objSubCategoria.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                        ListSubCategorias.Add(objSubCategoria);
                    }
                }
            }
            catch
            {
                ListSubCategorias = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return ListSubCategorias;
        }

        public GRSubCategoria ObtenerDatosSubCategoria(Int32 intCodSubCategoria)
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            GRSubCategoria objSubCategoria = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GRSubCategoria_Obtener", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@iCodSub", SqlDbType.Int).Value = intCodSubCategoria;
                rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        objSubCategoria = new GRSubCategoria();
                        objSubCategoria.iCodSub = Convert.ToInt32(rdr["iCodSub"].ToString());
                        objSubCategoria.iCodCat = Convert.ToInt32(rdr["iCodCat"].ToString());
                        objSubCategoria.vNomSub = rdr["vNomSub"].ToString();
                        objSubCategoria.vDesSub = rdr["vDesSub"].ToString();
                        objSubCategoria.bActivoSub = Convert.ToBoolean(rdr["bActivoSub"].ToString());
                        objSubCategoria.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objSubCategoria.cNomTer = rdr["cNomTer"].ToString();
                        objSubCategoria.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                    }
                }
            }
            catch
            {
                objSubCategoria = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return objSubCategoria;
        }

        public List<GRSubCategoria> ObtenerSubCatXCategoria(Int32 intCodCategoria)
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            List<GRSubCategoria> ListSubCategorias = null;
            GRSubCategoria objSubCategoria = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GRSubCategoria_x_Categoria", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@iCodCat", SqlDbType.Int).Value = intCodCategoria;
                rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    ListSubCategorias = new List<GRSubCategoria>();
                    while (rdr.Read())
                    {
                        objSubCategoria = new GRSubCategoria();
                        objSubCategoria.iCodSub = Convert.ToInt32(rdr["iCodSub"].ToString());
                        objSubCategoria.iCodCat = Convert.ToInt32(rdr["iCodCat"].ToString());
                        objSubCategoria.vNomSub = rdr["vNomSub"].ToString();
                        objSubCategoria.vDesSub = rdr["vDesSub"].ToString();
                        objSubCategoria.bActivoSub = Convert.ToBoolean(rdr["bActivoSub"].ToString());
                        objSubCategoria.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objSubCategoria.cNomTer = rdr["cNomTer"].ToString();
                        objSubCategoria.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                        ListSubCategorias.Add(objSubCategoria);
                    }
                }
            }
            catch
            {
                ListSubCategorias = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return ListSubCategorias;
        }

    }
}